package com.xhdx.intern;
/**
 * 
 凡是有双引号括起字符串的地方就会用到ldc指令,比如String a = "hello"。
intern的作用是把new出来的字符串的引用添加到stringtable中，java会先计算string的hashcode，查找stringtable中是否已经有string对应的引用了，如果有返回引用（地址），然后没有把字符串的地址放到stringtable中，并返回字符串的引用（地址）
 * @author lenovo
 *
 */
public class TestIntern {
    public static void main(String[] args) {
        String abc = new String("test");
        System.out.println("hashcode = "+abc.hashCode());
        System.out.println(abc.intern() == abc);//false
        System.out.println("hashcode = "+abc.hashCode());
        
        String ebc = new String("jo") + new String("hn");
        System.out.println(ebc.intern() == ebc);//true
        System.out.println("=========================================== = ");
        String str1 = "a";
        String str2 = "b";
        String str3 = "ab";
        String str4 = str1 + str2;
        String str5 = new String("ab");
         
        System.out.println(str5.equals(str3));
        System.out.println(str5 == str3);
        System.out.println(str5.intern() == str3);
        System.out.println(str5.intern() == str4);
        System.out.println("=========================================== = ");
        String a = "abc";
        String b = "abc";
        String c = "a" + "b" + "c";
        String d = "a" + "bc";
        String e = "ab" + "c";
                
        System.out.println(a == b);
        System.out.println(a == c);
        System.out.println(a == d);
        System.out.println(a == e);
        System.out.println(c == d);
        System.out.println(c == e);
    }
}
